package com.itts.Factory;

import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

public abstract class Validator<S> extends ValidatorMethod {

    public Validator(){}
    public abstract List<String> validate(S source);

    public List<String> validateList(List<S> source){
        List<String> errorList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(source)){
            Iterator<S> iterator = source.iterator();
            while (iterator.hasNext()){
                S s = iterator.next();
                errorList.addAll(this.validate(s));
            }
        }else {
            errorList.add("请求报文主体为空");
        }
        return (List) errorList.stream().distinct().collect(Collectors.toList());
    }

    public List<String> validate(Req<S> req){
        List<String> errorList = new ArrayList<>();
        errorList.addAll(this.validate(req.getBody()));
        return (List) errorList.stream().distinct().collect(Collectors.toList());
    }


}
